local Lang = require("common.Lang")
local Msg = require("core.Msg")
local Broadcast = require("broadcast.Broadcast")
local PanelDefine = require("broadcast.PanelDefine")
local Grid = require("bag.Grid")
local ItemDefine = require("bag.ItemDefine")
local ShopConfig = require("excel.Shop")
local ServerCommonFL = require("serverCommon.ServerCommonFL")

SHOP_LIMIT_BUY_MAX_CNT = 15 * 60
LIMIT_BUY_REMAIN_CNT = LIMIT_BUY_REMAIN_CNT or {}

-- 元宝不足时候的处理
function onNoRMB(human, arg1, arg2)
	if human.db.rmb == 0 then
		Broadcast.sendErr(human, Lang.SHOP_ERR_YUANBAO)
	
		local msgRet = Msg.gc.GC_OPEN_PANEL
		msgRet.id = PanelDefine.PANEL_FIRST_DELTA
		msgRet.values[0] = 0
		Msg.send(msgRet, human.fd)
	else
		local sendMsg = Msg.gc.GC_NO_RMB
		sendMsg.arg1 = arg1 or 0
		sendMsg.arg2 = arg2 or 0
		Msg.send(sendMsg, human.fd)
	end
end
--设置限购信息
function setLimit(human, config, left, shopIndex, i)
  if config.limitCnt and shopIndex == 7 then    
    left.limitCnt = config.maxCnt - LIMIT_BUY_REMAIN_CNT[i]
    left.totalCnt = 0
  elseif config.limitCnt then
    left.limitCnt = math.floor(config.limitCnt) - human.db.shopLimtiCnt[(shopIndex - 1) * 60 + i - 1]
    left.totalCnt = math.floor(config.limitCnt)
  else
    left.limitCnt = 0
    left.totalCnt = 0
  end
end
--得到钜惠的具体配置表
function getRealPrivilegeConfig()  
  local openTime = ServerCommonFL.getHadOpenDay() % 10
  if ServerCommonFL.getHadOpenDay() >= 9 then
    return ShopConfig.privilege4
  end
  if openTime == 0 or openTime == 9 then
    return ShopConfig.privilege4
  elseif openTime == 1 or openTime == 2 then
    return ShopConfig.privilege
  elseif openTime == 3 or openTime == 4 then
    return ShopConfig.privilege1
  elseif openTime == 5 or openTime == 6 then
    return ShopConfig.privilege2
  elseif openTime == 7 or openTime == 8 then
    return ShopConfig.privilege3
  end
end
--设置商品信息
function setGoods(human, msg, mallConfig, index)
    msg[0] = 0
    local cache = {}
    if index == 1 then
      mallConfig = getRealPrivilegeConfig()
    end
    for i=1, #mallConfig do
        msg[0] = msg[0] + 1
        local left = msg[msg[0]]
        left.oldPrice = mallConfig[i].oldPrice or 0
        left.price = mallConfig[i].price
        Grid.create(cache, mallConfig[i].id, 1)
        Grid.makeItem(cache, left.grid, 0, human)
        left.type = mallConfig[i].showType or 0
        left.shop_id = index
        setLimit(human, mallConfig[i], left, index, i)
        left.limitTime = 0
        left.money_type_item_icon = 0
        left.money_type_item_name = ""
        left.showPos = mallConfig[i].showPos or 0
        local cf = ItemDefine.getCf(mallConfig[i].money_type)
        if cf then
            left.money_type_item_icon =cf.icon
            left.money_type_item_name = cf.name
        end

        left.money_type = mallConfig[i].money_type
    end
end